package com.haohan.stock.service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import com.haohan.stock.domain.StockData;
import com.haohan.util.CodeList;
import com.haohan.util.DataUtil;

/**
 * 奖项数据操作类
 * 
 * @author Administrator
 *
 */
@Service
public class StockaggregateService {

	public void saveinit(int scale) throws IOException {
		String[] a = CodeList.codes;
		for (int i = 0; i < a.length; i++) {
			System.out.println("BEGIN===" + (i + 1) + "-------------开始保存" + a[i]);
			String str = saveOne(a[i], scale);
			FileUtils.write(new File("F:\\all.json"), str.replaceFirst("]", ""), "UTF-8", true);
			System.out.println("END===" + (i + 1) + "-------------结束保存" + a[i]);
		}
		FileUtils.write(new File("F:\\all.json"), "]", "UTF-8", true);
	}

	private String saveOne(String code, int scale) {
		List<StockData> list = DataUtil.getStockData(code, scale, 100000);
		if (list == null || list.isEmpty()) {
			return "";
		}
		calcPrice(list, 5);
		calcPrice(list, 10);
		calcPrice(list, 20);
		calcPrice(list, 30);
		calcVolume(list, 5);
		calcVolume(list, 10);
		calcVolume(list, 20);
		calcVolume(list, 30);
		calcPoint(list);
		setid(list, code, scale);
		calcCount(list);
		return DataUtil.objectToJson(list);
	}
	public void calcCount(List<StockData> list){
		Map<Integer,StockData> map = new HashMap<Integer,StockData>();
		Map<String,Integer> mapInt = new HashMap<String,Integer>();
		for (int i = 0; i < list.size(); i++) {
			map.put(i+1, list.get(i));
		}
		for (int i = list.size(); i >0; i--) {
			if(checkIsAbove(map.get(i))&&map.get(i-1)!=null&&!checkIsAbove(map.get(i-1))){
				int k=0;
				for (int j=i; j>0; j--) {
					k++;
					if(checkIsBelow(map.get(j))&&map.get(j-1)!=null&&!checkIsBelow(map.get(j+1))){
						break;
					}
				}
				mapInt.put(map.get(i).getDay(), k);
			}
		}
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData stockData = (StockData) iterator.next();
			if(mapInt.get(stockData.getDay())!=null){
				stockData.setCount(mapInt.get(stockData.getDay()));
			}
		}
	}
	private boolean checkIsAbove(StockData stockData ){
		return stockData.getClose() >= stockData.getPrice5() && stockData.getClose() >=stockData.getPrice10()
				&& stockData.getClose() >= stockData.getPrice20() && stockData.getClose() >= stockData.getPrice30();
				
	}
	private boolean checkIsBelow(StockData stockData ){
		return stockData.getClose() < stockData.getPrice5() && stockData.getClose() <stockData.getPrice10()
				&& stockData.getClose() < stockData.getPrice20() && stockData.getClose() < stockData.getPrice30();
	}

	private void setid(List<StockData> list, String code, int scale) {
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData stockData = (StockData) iterator.next();
			stockData.setCode(code);
			stockData.setScale(scale);
		}
	}

	private void calcPoint(List<StockData> list) {
		Integer i = 0;
		Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
		Map<Integer, Boolean> mapSecond = new HashMap<Integer, Boolean>();
		Map<Integer, Boolean> mapSellSecond = new HashMap<Integer, Boolean>();
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData stockData = (StockData) iterator.next();
			i++;
			boolean checkModern = false;
			if (stockData.getClose() > stockData.getPrice5() && stockData.getClose() > stockData.getPrice10()
					&& stockData.getClose() > stockData.getPrice20() && stockData.getClose() > stockData.getPrice30()) {
				checkModern = true;
			} else {
				checkModern = false;
				mapSecond.put(i, true);
			}
			if (stockData.getClose() < stockData.getPrice5() && stockData.getClose() < stockData.getPrice10()
					&& stockData.getClose() < stockData.getPrice20() && stockData.getClose() < stockData.getPrice30()) {
				mapSellSecond.put(i, true);
			} else {
				mapSellSecond.put(i, false);
			}
			if (stockData.getClose() < stockData.getPrice5() && stockData.getClose() < stockData.getPrice10()
					&& stockData.getClose() < stockData.getPrice20() && stockData.getClose() < stockData.getPrice30()) {
				map.put(i, true);
			} else {
				map.put(i, false);
			}
			if (map.get(i - 1) != null && map.get(i - 1) && checkModern) {
				stockData.setBuy_point(true);
			}
			if (mapSecond.get(i - 1) != null && mapSecond.get(i - 1) && checkModern) {
				stockData.setBuy_point_second(true);
			}
			if (mapSellSecond.get(i) != null&&mapSellSecond.get(i - 1) != null && !mapSellSecond.get(i - 1) && mapSellSecond.get(i)) {
				stockData.setSell_point_second(true);
			}
			if((stockData.getClose() >= stockData.getPrice5() && stockData.getClose() >=stockData.getPrice10()
					&& stockData.getClose() >= stockData.getPrice20() && stockData.getClose() >= stockData.getPrice30())&&
					(stockData.getLow() <= stockData.getPrice5() && stockData.getLow() <=stockData.getPrice10()
							&& stockData.getLow()  <=stockData.getPrice20() && stockData.getLow()  <=stockData.getPrice30())){
				stockData.setOneyang_wears_yin(true);
			}
		}
	}

	private void calcPrice(List<StockData> list, int param) {
		Map<String, Double> map = new HashMap<String, Double>();
		for (int i = 0; i < list.size(); i++) {
			double pricema = 0d;
			if ((i + param - 1) >= list.size()) {
				break;
			}
			for (int j = i; j < i + param; j++) {
				pricema += list.get(j).getClose();
			}
			map.put(list.get(i + param - 1).getDay(), pricema / (param + 0d));
		}
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData StockThirtyMinutes = (StockData) iterator.next();
			if (map.get(StockThirtyMinutes.getDay()) == null) {
				continue;
			}
			if (param == 5) {
				StockThirtyMinutes.setPrice5(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 10) {
				StockThirtyMinutes.setPrice10(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 20) {
				StockThirtyMinutes.setPrice20(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 30) {
				StockThirtyMinutes.setPrice30(map.get(StockThirtyMinutes.getDay()));
			}
		}
	}

	private void calcVolume(List<StockData> list, int param) {
		Map<String, Double> map = new HashMap<String, Double>();
		for (int i = 0; i < list.size(); i++) {
			double pricema = 0d;
			if ((i + param - 1) >= list.size()) {
				break;
			}
			for (int j = i; j < i + param; j++) {
				pricema += list.get(j).getVolume();
			}
			map.put(list.get(i + param - 1).getDay(), pricema / (param + 0d));
		}
		for (Iterator<StockData> iterator = list.iterator(); iterator.hasNext();) {
			StockData StockThirtyMinutes = (StockData) iterator.next();
			if (map.get(StockThirtyMinutes.getDay()) == null) {
				continue;
			}
			if (param == 5) {
				StockThirtyMinutes.setVolume5(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 10) {
				StockThirtyMinutes.setVolume10(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 20) {
				StockThirtyMinutes.setVolume20(map.get(StockThirtyMinutes.getDay()));
			}
			if (param == 30) {
				StockThirtyMinutes.setVolume30(map.get(StockThirtyMinutes.getDay()));
			}
		}
	}
}
